package com.android.systemui.ios.views;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.os.Vibrator;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.CycleInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.systemui.ios.Constants;
import com.android.systemui.ios.R;

import java.util.Iterator;
import java.util.LinkedList;

public class PasswordInputView extends LinearLayout implements View.OnClickListener {

    private final int MAX_CHAR = 6;

    private ImageView[] mImageResult = new ImageView[MAX_CHAR];
    private Button[] mInputButtons = new Button[10];
    private TextView mBtnEmergency;
    private TextView mBtnDelete;
    private View mResultLayout;

    private final LinkedList<String> mNumberInputLink = new LinkedList<>();

    private Vibrator mVibrator;
    private OnInputChangedListener mListener;

    public PasswordInputView(Context context) {
        this(context, null, 0);
    }

    public PasswordInputView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PasswordInputView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mVibrator = context.getSystemService(Vibrator.class);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        mResultLayout = findViewById(R.id.input_result_layout);

        mImageResult[0] = findViewById(R.id.iv_result_1);
        mImageResult[1] = findViewById(R.id.iv_result_2);
        mImageResult[2] = findViewById(R.id.iv_result_3);
        mImageResult[3] = findViewById(R.id.iv_result_4);
        mImageResult[4] = findViewById(R.id.iv_result_5);
        mImageResult[5] = findViewById(R.id.iv_result_6);

        mInputButtons[0] = findViewById(R.id.btn_input_0);
        mInputButtons[1] = findViewById(R.id.btn_input_1);
        mInputButtons[2] = findViewById(R.id.btn_input_2);
        mInputButtons[3] = findViewById(R.id.btn_input_3);
        mInputButtons[4] = findViewById(R.id.btn_input_4);
        mInputButtons[5] = findViewById(R.id.btn_input_5);
        mInputButtons[6] = findViewById(R.id.btn_input_6);
        mInputButtons[7] = findViewById(R.id.btn_input_7);
        mInputButtons[8] = findViewById(R.id.btn_input_8);
        mInputButtons[9] = findViewById(R.id.btn_input_9);

        for (int i = 0; i < 10; i++) {
            mInputButtons[i].setOnClickListener(this);
        }

        mBtnDelete = findViewById(R.id.btn_delete);
        mBtnEmergency = findViewById(R.id.btn_emergency);

        mBtnDelete.setOnClickListener(this);
        mBtnEmergency.setOnClickListener(this);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return true;
    }

    @Override
    public void onClick(View v) {
        final int id = v.getId();
        if (id == R.id.btn_emergency) {
            if (mListener != null) {
                mListener.onEmergencyCall();
            }
        } else if (id == R.id.btn_delete) {
            if (mNumberInputLink.size() > 0) {
                mNumberInputLink.removeLast();
                updateImageResult();
            } else {
                if (mListener != null) {
                    mListener.onCancel();
                }
            }
        } else {
            if (v instanceof Button) {
                if (mNumberInputLink.size() < MAX_CHAR) {
                    String val = ((Button) v).getText().toString();
                    mNumberInputLink.add(val);
                    updateImageResult();
                    if (mVibrator.hasVibrator()) {
                        mVibrator.vibrate(30);
                    }
                    if (mNumberInputLink.size() == MAX_CHAR) {
                        inputFinish();
                    }
                } else {
                    inputFinish();
                }
            }
        }
    }

    public void resetAll() {
        mNumberInputLink.clear();
        updateImageResult();
    }

    public void verifyError() {
        resetAll();
        ObjectAnimator shakeAnimator = ObjectAnimator.ofFloat(mResultLayout, "translationX", 0, 16 * Constants.screenDensity);
        shakeAnimator.setDuration(300);
        shakeAnimator.setInterpolator(new CycleInterpolator(2));
        shakeAnimator.start();
        if (mVibrator.hasVibrator()) {
            long[] pattern = {100, 10, 100, 10, 100};
            mVibrator.vibrate(pattern, -1);
        }
    }

    private void updateImageResult() {
        int size = mNumberInputLink.size();
        for (int i = 0; i < 6; i++) {
            mImageResult[i].setSelected(i < size);
        }
        if (size == 0) {
            mBtnDelete.setText(android.R.string.cancel);
        } else {
            mBtnDelete.setText(R.string.title_delete);
        }
    }

    private void inputFinish() {
        if (mListener != null) {
            Iterator<String> it = mNumberInputLink.iterator();
            if (it.hasNext()) {
                StringBuilder builder = new StringBuilder();
                do {
                    builder.append(it.next());
                } while (it.hasNext());
                mListener.onVerify(builder.toString());
            }
        }
    }

    public void setOnInputChangedListener(OnInputChangedListener listener) {
        mListener = listener;
    }

    public interface OnInputChangedListener {
        void onCancel();

        void onVerify(String str);

        void onEmergencyCall();
    }
}
